package com.rapiddevsample;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.v.rapiddev.adpater.DynNewsAdapter;
import com.v.rapiddev.adpater.RecyclerItemClickListener;
import com.v.rapiddev.base.AppManager;
import com.v.rapiddev.swipebacklayout.SwipeBackActivity;
import com.v.rapiddev.utils.Utils_RapidDev;
import com.v.rapiddev.utils.V;

import net.bither.util.NativeUtil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.iwf.photopicker.PhotoPicker;
import me.iwf.photopicker.PhotoPreview;
import me.iwf.photopicker.utils.PermissionsUtils;

/**
 * ClassType:
 * User:wenyunzhao
 * ProjectName:RapidDevSample
 * Package_Name:com.rapiddevsample
 * Created on 2016-11-24 16:03
 */
public class ViewPagerAndImageSerlectorAndCompressSimpleDemo extends BaseActivity {
    @Bind(R.id.roll_view_pager)
    RollPagerView rollViewPager;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    private ArrayList<String> selectedPhotos = new ArrayList<>();
    private PhotoAdapter photoAdapter;
    private Activity context;


    @Override
    public void init() {
        setContentView(R.layout.aaaa);
        ButterKnife.bind(this);
        context = this;
        photoAdapter = new PhotoAdapter(this, selectedPhotos, false);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        recyclerView.setAdapter(photoAdapter);
        recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                PhotoPreview.builder()
                        .setPhotos(selectedPhotos)
                        .setCurrentItem(position)
                        .setShowDeleteButton(true)
                        .start(context);
            }
        }));
        initAdapter();
    }

    @Override
    public void resume() {

    }

    @Override
    public void destroy() {
        AppManager.getAppManager().finishActivity(this);
    }

    public void selectorPhoto(View view) {
        PhotoPicker.builder()
                .setPhotoCount(9)
                .setShowCamera(true)
                .setSelected(selectedPhotos)
                .start(this);
    }

    public void compressImg(View view) {
        //压缩图片
        ArrayList<String> list = photoAdapter.getList();
        V.d("共 " + list.size() + " 张图片");
        compress(context, list, new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.arg1 == 9000) {
                    V.d("压缩完成");
                }
            }
        });
    }

    public static void compress(final Activity context, final ArrayList<String> list, final Handler handler) {


        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < list.size(); i++) {
                    Bundle bundle = new Bundle();
                    Message message = handler.obtainMessage();
                    try {
                        bundle.putString("path", compressImage(context, Uri.fromFile(new File(list.get(i)))));
                        bundle.putInt("index", i);
                        message.arg1 = 9000;
                    } catch (Exception e) {
                        message.arg1 = -1;
                    }
                    message.setData(bundle);
                    handler.sendMessage(message);
                }

            }
        }).start();


    }


    public static String compressImage(Activity activity, final Uri uri) {

        V.d("************************华丽的分割线************************");
        File saveFile = new File(activity.getExternalCacheDir(), "compress_" + System.currentTimeMillis() + ".jpg");
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), uri);
            V.d(">>>>>>>>>>>>>>>>>压缩前路径：" + uri.getPath() + ",大小：" + bitmap.getByteCount() + "<<<<<<<<<<<<<<<<<<<");
            V.d("=>=>=>=>=>=>=>=>" + "开始压缩" + "<=<=<=<=<=<=<=<=<=<=");
            NativeUtil.compressBitmap(bitmap, saveFile.getAbsolutePath());
            V.d("=>=>=>=>=>=>=>=>" + "压缩完成" + "<=<=<=<=<=<=<=<=<=<=");
            V.d(">>>>>>>>>>>>>>>>>压缩后路径：" + saveFile.getAbsolutePath() + ",大小：" + Utils_RapidDev.getBitmapFromPath(saveFile.getAbsolutePath()).getByteCount() + "<<<<<<<<<<<<<<<<<<<");
            bitmap = null;
        } catch (IOException e) {
            e.printStackTrace();
            V.e("压缩出错" + e.toString());
        }
        V.d("************************华丽的分割线************************");
        return saveFile.getAbsolutePath();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Utils.setSelectorImgOnResult(requestCode, resultCode, data, photoAdapter, selectedPhotos);
    }

    private void initAdapter() {
        List<String> list = new ArrayList<>();
        list.add("http://f.hiphotos.baidu.com/image/pic/item/bba1cd11728b47101489df48c0cec3fdfd03238b.jpg");
        list.add("http://f.hiphotos.baidu.com/image/pic/item/203fb80e7bec54e753da379aba389b504fc26a7b.jpg");
        list.add("http://g.hiphotos.baidu.com/image/pic/item/f703738da97739125daca7e5fb198618377ae2a8.jpg");
        list.add("http://e.hiphotos.baidu.com/image/pic/item/738b4710b912c8fc7778d223f8039245d7882150.jpg");
        list.add("http://b.hiphotos.baidu.com/image/pic/item/77c6a7efce1b9d168427be8ff7deb48f8d54647f.jpg");
        rollViewPager.setAdapter(new DynNewsAdapter<>(list));
        rollViewPager.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                V.d("--position:" + position);
            }
        });
    }
}
